package homework;

public class Test04 {
    //2.编写多线程程序，模拟多个人通过一个山洞的模拟。这个山洞每次只能通过一个人，
    // 每个人通过山洞的时间为5秒，有10个人同时准备过此山洞，显示每次通过山洞人的姓名和顺序。
    public static void main(String[] args) {
        //创建通过对象
        MyThread myThread = new MyThread();
        //创建对象
        String[] names = {"张三","李四","王五","赵六","孙七","周八","吴九","小十","小十一","小十二"};
        
        //创建线程
        for (String name : names) {
            Thread t1 = new Thread(() -> {
                myThread.pass(name);
            },name);
            t1.start();
        }

    }
}
class MyThread{
    private int p = 0;
    public synchronized void pass(String name) {
        p++;
        System.out.println( "第" + p + "号"+name + "正在通过山洞");
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(name + "已经通过山洞");
    }
}

